home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: how to get weeknumber according calendar
- Date: Fri, 05 Apr 96 14:35:32 GMT
- Organization: none
- Message-ID: <828714932snz@genesis.demon.co.uk>
- References: <4jt3jr$j58@hdxx05.telecom.ptt.nl>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4jt3jr$j58@hdxx05.telecom.ptt.nl>
- a.broers@ptt-telecom.unisource.nl "Andre Broers" writes:
-
- >Hello all,
- >
- >Hopw can I get a weeknumber following the calendar. This means the
- >first days in januari can be week 52, 53 or 1 (and not 0).
-
- That doesn't pin an algorithm down very much. However you may be referring
- to ISO 8601 week numbers. I have submitted the following for the next
- release of Snippets:
-
-
- /******************************************************************************
- * weeknum_ISO8601.c December 1995 L.Kirby
- *
- * Calculates the week number of a day in the year based on the ISO8601 week
- * numbering scheme. The arguments are:
- *
- * t - a pointer to a struct tm in which the following members must be set and
- * normalised to the standard ranges specified (as standard library
- * functions gmtime, localtime and mktime should do):
- *
- * tm_wday - The day of the week of the day in question:
- * 0-Sunday -> 6-Saturday
- *
- * tm_yday - The day of the year of the day in question: 0 -> 365
- * January 1st is day 0.
- *
- * tm_year - The year since 1900.
- *
- * firstdow - This defines the day of the week on which a week starts:
- * 0-Sunday -> 6-Saturday. For normal ISO8601 weeks that start on
- * a Monday this should be 1.
- *
- ******************************************************************************
- *
- * The week number is a value between 1 and 53 inclusive defined according to
- * the following rules:
- *
- * 1. The Gregorian calendar is assumed.
- *
- * 2. There are always 7 consecutive days with the same week number.
- *
- * 3. January 4th is defined to be in week 1. Equivalently week 1 is the
- * first week of a year which has at least 4 days in that year.
- *
- * 4. firstdow defines the day of the week which starts a new week i.e. has
- * a different week number from the previous day.
- *
- * 5. Week numbers increase in sequence from week 1 until the week that is
- * defined to be week 1 of the following year.
- *
- * It follows that:
- *
- * 6. Up to January 3rd may be in either week 1 of the current year or in
- * weeks 52 or 53 of the previous year.
- *
- * 7. 29th December onwards may be in either weeks 52 or 53 of the current
- * year or week 1 of the following year.
- *
- ******************************************************************************
- */
-
- #include <time.h>
-
- int weeknum_ISO8601(const struct tm *t, int firstdow)
-
- {
- const int tmp1 = firstdow - t->tm_wday;
- const int tmp2 = t->tm_yday + ((tmp1 > 0) ? 3 : 10) + tmp1;
- const int fourthdaynum = tmp2 % 7;
- int week = tmp2 / 7;
-
- if (week == 0) {
- const int year = t->tm_year + 1900 - 1;
- const int isleap = !(year % 4) && ((year % 100) || !(year % 400));
-
- week = (fourthdaynum + isleap >= 6) ? 53 : 52;
- } else if (week == 53) {
- const int year = t->tm_year + 1900;
- const int isleap = !(year % 4) && ((year % 100) || !(year % 400));
-
- if (fourthdaynum > isleap)
- week = 1;
- }
-
- return week;
- }
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-